package com.zzyl.nursing.controller.member;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{

    @Autowired
    private IReservationService reservationService;

    /**
     * 查询取消预约数量
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public AjaxResult getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        int count = reservationService.getCancelledReservationCount(userId);
        return success(count);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List<TimeCountVo>> countByTime(@RequestParam Long time) {
        List<TimeCountVo> countList = reservationService.countByTime(time);
        return R.ok(countList);
    }

    /**
     * 查询预约信息列表
     */
    @ApiOperation("查询预约信息列表")
    @GetMapping("/page")
    public AjaxResult list(@ApiParam("查询条件对象") Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return success(getDataTable(list));
    }

    /**
     * 导出预约信息列表
     */
    @ApiOperation("导出预约信息列表")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam("导出的查询条件") HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    @ApiOperation("获取预约信息详细信息")
    @GetMapping(value = "/{id}")
    public R<Reservation> getInfo(@PathVariable("id") @ApiParam("预约信息ID") Long id)
    {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
    @ApiOperation("新增预约信息")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @ApiParam("新增的预约信息对象") Reservation reservation)
    {
        // 检查是否已存在相同手机号和时间的预约
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reservation::getMobile, reservation.getMobile())
                  .eq(Reservation::getTime, reservation.getTime());
        if (reservationService.getOne(queryWrapper) != null) {
            return AjaxResult.error("该时间段已存在相同手机号的预约");
        }
        
        // 设置默认状态为0（待报道）
        reservation.setStatus(0);
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @ApiOperation("修改预约信息")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @ApiParam("修改的预约信息对象") Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@PathVariable("id") @ApiParam("预约信息ID") Long id) {
        return toAjax(reservationService.cancel(id));
    }
}